package com.mindainfo.groundwave.web.member.web;

import com.alibaba.fastjson.JSON;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.api.member.service.IStafferService;
import com.mindainfo.groundwave.common.api.member.vo.DeptVo;
import com.mindainfo.groundwave.common.api.member.vo.StafferVo;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.DataGrid;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by MD-01 on 2017/5/27.
 */
@Controller
@RequestMapping("/sys/staffer")
public class StafferController {

    @Resource
    private IStafferService stafferService;
    @Resource
    private IDeptService deptService;

    @RequestMapping("/page")
    public String page(HttpServletRequest request){

        //CommonResult<List<DeptVo>> result = deptService.myDepts();
        //request.setAttribute("depts",result.getData());
        return "/member/staffer";
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public DataGrid<StafferVo> queryList(Integer page,Integer rows,
                                                       String key){
        if(page==null){
            page = 1;
        }
        if(rows==null){
            rows = 10;
        }
        int start = (page-1)*rows;
        return stafferService.queryList(start,rows,key).getData();
    }

    @RequestMapping("/add")
    @ResponseBody
    public CommonResult<Void> add(Integer staffId,String name,String phoneNum,Integer deptId){

        System.out.println(staffId);
        return stafferService.add(staffId,name,phoneNum,deptId);
    }

    @RequestMapping("/queryUsableAll")
    @ResponseBody
    public CommonResult<Map<String,List<StafferVo>>> queryUsableAll(){

        CommonResult<Map<String, List<StafferVo>>> mapCommonResult = stafferService.queryUsableAll();

        System.out.println(JSON.toJSONString(mapCommonResult));
        return mapCommonResult;
    }

    @RequestMapping("/queryByDeptId")
    @ResponseBody
    public CommonResult<List<StafferVo>> queryByDeptId(Integer deptId){

        return stafferService.queryByDeptId(deptId);
    }
}
